package com.taptech.doufu.ui.viewholder;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taptech.doufu.util.ScreenUtil2;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.MoreAlbumsActivity;
import com.taptech.doufu.bean.AlbumsBean;
import com.taptech.doufu.bean.NewHomeBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.ui.viewholder.personalcenter.BaseAdapterViewHolder;
import com.taptech.doufu.util.TMAnalysis;
import com.taptech.doufu.ui.view.ImageSubjectViewGroup;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * Created by lsy on 2015/9/1.
 */
public class NewHomeSubjectItemViewHolder extends BaseAdapterViewHolder implements View.OnClickListener
{

    static final public String SUBJECT_TYPE_POPULAR = "介绍专题";
    static final public String SUBJECT_TYPE_TOPIC = "";
    static final public String SUBJECT_TYPE_DRAW = "介绍绘画";
    static final public String SUBJECT_TYPE_NOVEL = "介绍小说";
    static final public String SUBJECT_TYPE_COMMON = "热门精选";

    final private int SUBJECT_MARGIN_TOP = ScreenUtil2.dip2px(8);
    final private int SUBJECT_MARGIN_BOTTOM = 0;
    final private int SUBJECT_MARGIN_LEFT = ScreenUtil2.dip2px(8);
    final private int SUBJECT_MARGIN_RIGHT = ScreenUtil2.dip2px(8);
    private Context mContext;
    private Handler mHandler;

    // UI 控件
    RelativeLayout mSubjectLayout;
    RelativeLayout mSubjectTitleLayout;
    TextView mSubjectLayoutBackground;
    TextView mSubjectTypeTitle;
    ImageSubjectViewGroup mSubjectImageItem;
    TextView mEnterAllSubjectBtn;
    ImageView mHomeMoreIcon;

    // 数据
    NewHomeBean bean;
    String mTitle;
    List<AlbumsBean> albumBeans;
    private TextView subject_title;

    public NewHomeSubjectItemViewHolder(Context context, int position)
    {
        super(context, position, R.layout.home_subject_item_viewholder_layout);
        mContext = context;
    }

    public NewHomeSubjectItemViewHolder(Context context, int position, int layoutId)
    {
        super(context, position, layoutId);
        mContext = context;
    }

    public void setPosition(int position)
    {
        mPositon = position;
    }

    public void setTitle(String title)
    {
        mTitle = title;
    }

    @Override
    public BaseAdapterViewHolder createViewHolder(Context context, int position)
    {
        return null;
    }

    @Override
    public void getChildView(View convertView)
    {
        mSubjectLayout = (RelativeLayout) convertView.findViewById(R.id.subject_backgroud_layout);
        mSubjectTitleLayout = (RelativeLayout) convertView.findViewById(R.id.subject_title_layout);
        mSubjectTypeTitle = (TextView) convertView.findViewById(R.id.subject_title);
        mSubjectImageItem = (ImageSubjectViewGroup) convertView.findViewById(R.id.subject_content_list);
        mEnterAllSubjectBtn = (TextView) convertView.findViewById(R.id.enter_all_subject_btn);
        mSubjectLayoutBackground = (TextView) convertView.findViewById(R.id.subject_backgroud_draw);
        mHomeMoreIcon = (ImageView) convertView.findViewById(R.id.enter_more_icon);
        subject_title = (TextView) convertView.findViewById(R.id.subject_title);
    }

    @Override
    public void setChildView(Object object,int singlePage)
    {
        if (object == null)
        {
            return;
        }
        bean = (NewHomeBean) object;
        if (bean == null)
        {
            return;
        }

        mHomeMoreIcon.setOnClickListener(this);

        Class<?> classType = bean.getClass();
        // 获取数据
        // 获得和属性对应的getXXX()方法
        String getFieldName = "get" + mTitle.substring(0, 1).toUpperCase() + mTitle.substring(1);
        try
        {
            Method getMedthod = classType.getMethod(getFieldName, new Class[]{});
            albumBeans = (List<AlbumsBean>) getMedthod.invoke(bean, new Object[]{});
            for (int i = 0; i < albumBeans.size(); i++)
            {
                AlbumsBean albumsBean = albumBeans.get(i);
                if (albumsBean != null && mTitle != null && mTitle.equals("tag_big"))
                {
                    albumsBean.setIsNovelSingel(true);
                    albumsBean.setIsTagSingle(true);
                }
               /* if (albumsBean != null && mTitle != null && (mTitle.equals("drawing_big") || mTitle.equals("novel_big")))
                {
                    albumsBean.setIsNovelSingel(true);
                }*/
            }
            /*for (int i = 0; i < albumBeans.size(); i++)
            {
                AlbumsBean albumsBean = albumBeans.get(i);
                if (albumsBean != null && mTitle != null && mTitle.equals("part5_albums"))
                {
                    albumsBean.setRank_flag(true);
                } else if (albumsBean != null && mTitle != null && mTitle.equals("signed_author_album"))
                {
                    albumsBean.setIsNovelSingel(true);
                } else if (albumsBean != null && mTitle != null && mTitle.equals("signed_author_novel_album"))
                {
                    albumsBean.setIsNovelSingel(true);
                } else if (albumsBean != null && mTitle != null && mTitle.equals("finished_novel_entry"))
                {
                    albumsBean.setIsNovelSingel(true);
                } else if (albumsBean != null && mTitle != null && mTitle.equals("novel_activity_albums"))
                {
                    subject_title.getPaint().setFakeBoldText(true);//加粗
                    subject_title.setTextColor(Color.parseColor("#858585"));
                }
            }*/
            mSubjectImageItem.setSubjectData(albumBeans);

            if (albumBeans != null)
            {

                RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
                // 若专题图片超过三张，将父窗口横向扩大到屏幕两端，使用假背景
                if (albumBeans.size() > 3)
                {
                    mSubjectLayout.setBackgroundColor(Color.parseColor("#00ffffff"));
                    mSubjectLayoutBackground.setVisibility(View.VISIBLE);
                    lparams.setMargins(0, SUBJECT_MARGIN_TOP, 0, SUBJECT_MARGIN_BOTTOM);
                    mSubjectTitleLayout.setPadding(0, 0, 0, 0);
                }
                // 否则，将假背景隐藏，将父窗口收缩至有边距位置，显示真背景
                else
                {
                    mSubjectLayout.setBackgroundResource(R.drawable.bg_home_sweep_item_up);
                    mSubjectLayoutBackground.setVisibility(View.GONE);
                    lparams.setMargins(SUBJECT_MARGIN_LEFT, SUBJECT_MARGIN_TOP, SUBJECT_MARGIN_RIGHT, SUBJECT_MARGIN_BOTTOM);
                    mSubjectTitleLayout.setPadding(0, 0, 0, 0);
                }
                mSubjectLayout.setLayoutParams(lparams);

                lparams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) ((ScreenUtil2.getScreenWidthPixel((Activity) mContext) - ScreenUtil2.dip2px(48) - SUBJECT_MARGIN_LEFT - SUBJECT_MARGIN_RIGHT) / 3.5 + ScreenUtil2.dip2px(93)));

                lparams.setMargins(SUBJECT_MARGIN_LEFT, 0, SUBJECT_MARGIN_RIGHT, 0);
                mSubjectLayoutBackground.setLayoutParams(lparams);

            }

            if (mTitle != null)
            {
                mSubjectTypeTitle.setText(getTitleByDataName(mTitle));
            }

        } catch (NoSuchMethodException e)
        {
            e.printStackTrace();
        } catch (InvocationTargetException e)
        {
            e.printStackTrace();
        } catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
        mEnterAllSubjectBtn.setOnClickListener(this);
    }

    @Override
    public void setChildView(Object object, String keyWord,int singlePage)
    {
        setChildView(object,singlePage);
    }


    @Override
    public void setHandler(Handler handler)
    {
        mHandler = handler;
    }


    /**
     * 根据数据名称获取专题标题
     *
     * @param dataName
     * @return
     */
    private String getTitleByDataName(String dataName)
    {
        if (dataName.equals("top_albums"))
        {
            mEnterAllSubjectBtn.setText("更多");
            return SUBJECT_TYPE_POPULAR;
        } else if (dataName.equals("tag_big"))
        {
            mEnterAllSubjectBtn.setText("");
            mSubjectTitleLayout.setVisibility(View.GONE);
            return SUBJECT_TYPE_TOPIC;
        } else if (dataName.equals("drawing_big"))
        {
            mEnterAllSubjectBtn.setText("");
            mEnterAllSubjectBtn.setVisibility(View.GONE);
            mHomeMoreIcon.setVisibility(View.GONE);
            return SUBJECT_TYPE_DRAW;
        } else if (dataName.equals("novel_big"))
        {
            mEnterAllSubjectBtn.setText("");
            mEnterAllSubjectBtn.setVisibility(View.GONE);
            mHomeMoreIcon.setVisibility(View.GONE);
            return SUBJECT_TYPE_NOVEL;
        } else if (dataName.equals("common_albums"))
        {
            mEnterAllSubjectBtn.setText("更多");
            return SUBJECT_TYPE_COMMON;
        }
        return "";
    }

    @Override
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.enter_more_icon:
            case R.id.enter_all_subject_btn:
                int objectType = 0;
                String url = null;
                Intent intent = new Intent(mContext, MoreAlbumsActivity.class);
                //UIUtil.toastMessage(mContext, "进入专题" + mTitle);
                if (mTitle.equals("top_albums"))
                {
                    objectType = MoreAlbumsActivity.HOT_ALBMUS;
                    url = "http://api.doufu.la/index.php/albums/items?id=94";
                    TMAnalysis.event(mContext, "home-popular-subject-more");
                } else if (mTitle.equals("common_albums"))
                {
                   /* objectType = MoreAlbumsActivity.NOVELSWEEP_ALBMUS;
                    url = "http://api.doufu.la/index.php/albums/items?id=95";
                    TMAnalysis.event(mContext, "home-common-subject-more");*/
                    objectType = MoreAlbumsActivity.COMMON_ALBMUS;
                    // url = MoreAlbumsActivity.NOVEL_MORE_ALBMUS;
                    url = "http://api.doufu.la/index.php/albums/items?id=98";
                    TMAnalysis.event(mContext, "home-common-subject-more");
                }
                intent.putExtra(Constant.URL, url);
                intent.putExtra(Constant.TYPE, objectType);
                mContext.startActivity(intent);
                break;
        }
    }
}
